package com.sa.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.sa.dto.LoginUser;
import com.sa.dto.Result;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author wangxiaohui
 * @date 2023/6/9 16:38
 */

@RestController
@RequestMapping("/app")
public class IndexController {


    @RequestMapping("/hello")
    public String hello() {
        return "Hello Word!";
    }


    /**
     * 该登录为非前后端分离项目的登录方式
     * 会自动将token放到Cookie中返回到前端，前端在访问时能够携带访问后端
     * @param userName
     * @param password
     * @return
     */
    @RequestMapping("/login")
    public String login(String userName, String password) {
        if ("w".equals(userName) && "p".equals(password)) {
            StpUtil.login(1000);
            return "登录成功";
        } else {
            return "登录失败";
        }
    }


    @RequestMapping("/doLogin")
    public Result<LoginUser> doLogin(String userName, String password) {
        if ("w".equals(userName) && "p".equals(password)) {
            StpUtil.login(1000);
            LoginUser loginUser = new LoginUser();
            loginUser.setUserName(userName);
            loginUser.setId("1000");
            loginUser.setToken(StpUtil.getTokenValue());
            return Result.ok(loginUser);
        } else {
            return Result.error();
        }
    }


    @RequestMapping("/check")
    public String check() {
        return "当前登录状态：" + StpUtil.getLoginId();
    }


    @RequestMapping("/logout")
    public String logout() {
        StpUtil.logout();
        return "退出成功";
    }

    @RequestMapping("/permissions")
    public List<String> permissions() {
        return StpUtil.getPermissionList();
    }

    @RequestMapping("/roles")
    public List<String> roles() {
        return StpUtil.getRoleList();
    }

}
